package utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import interfaces.IUpdate;
	
	public class Particle implements IUpdate
	{
		public var xVel					:	Number; 		// the x and y velocity of the particle
		public var yVel					:	Number;
		
		public var drag					:	Number; 		// the drag factor between 0 and 1, where 1 is no drag, and 0 is max drag. 
		
		public var fade					:	Number;		// the fade factor, works as a multiplier. 
		
		public var shrink				:	Number;		// another multiplier that changes the size. 
		// If < 1 then the particle graphic shrinks, >1 and the particle grows. 
		
		public var gravity				:	Number;		// the amount of gravity to add to the yVelocity every frame. If < 0 
		// then gravity goes upwards. 
		
		public var directionRotate:	Boolean; 		// if true then the particle points in the direction it's moving
		
		public var disabled	:	Boolean; 				// for disabling the particle so we can reuse it. 
		
		public var clip:DisplayObject;								// display object for the particle
		
		public var spin					: 	Number; //
		
		public function Particle(c:DisplayObject, targetclip:DisplayObjectContainer, xpos:Number = 0, ypos:Number = 0)
		{
			
			// make a new particle
			if(c && targetclip)
			{
				clip = c;
			
				// add it to the layer
			
				targetclip.addChild(clip); 
			
				// set the position
				setPos(xpos, ypos);
			
				// and the default settings
				clip.visible = true; 
				
			}
			// and the default settings
			initDefaults(); 
		}
		
		public function initDefaults():void
		{
			drag = 1; 
			fade = 1; 
			shrink = 1; 
			gravity = 0; 
			spin = 0;
			directionRotate = false; 
			enable(); 
			
		}
		
		public function setPos(x:Number, y:Number):void
		{
			clip.x = x; 
			clip.y = y;
		}
		
		public function setXPos(x:Number):void
		{
			clip.x = x;
		}
		
		public function setYPos(y:Number):void
		{
			clip.y = y;
		}
		
		public function setVel(xvel:Number, yvel:Number):void
		{
			xVel = xvel;
			yVel = yvel;
		}
		
		public function setScale(newscale:Number):void
		{
			clip.scaleX = clip.scaleY = newscale;
		}
		
		public function update(e:Object = null):void
		{
			if(disabled) return ; 
			// add the velocity to the particle's position... 
			clip.x += xVel;
			clip.y += yVel;
			
			// apply drag
			xVel*=drag;
			yVel*=drag;
			
			// fade out
			clip.alpha = ((clip.alpha*1000)*fade)/1000;
			
			// scale
			clip.scaleX = clip.scaleY *=shrink;
			
			// gravity
			yVel+=gravity;
			
			// rotation
			clip.rotation += spin;
			
			if(directionRotate) {
				
				updateRotation(); 
			}
		}
		
		public function restart(x:Number, y:Number):void
		{
			initDefaults(); 
			clip.visible = true; 
			disabled = false; 
			setPos(x,y); 
		}
		
		public function updateRotation():void
		{
			//clip.rotation = Math.atan2(yVel,xVel)*(180/Math.PI); 
			clip.rotation += Math.atan2(yVel,xVel)*(180/Math.PI); 
		}
		
		public function disable():void
		{
			if(clip)
				clip.visible = false; 
			disabled = true; 
			
		}
		public function enable():void
		{
			if(clip)
				clip.visible = true;
			disabled = false; 
			
		}
		
		public function destroy():void
		{
			if(clip.parent)
				clip.parent.removeChild(clip);
			clip = null; 
		}
		
		
		public function set start(b:Boolean):void
		{
		}
		
		public function get start():Boolean
		{
			return false;
		}
	}
}